package me.buick.util.jmeter.snmpvisualizers;

import static javax.swing.SwingConstants.CENTER;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import org.apache.jmeter.visualizers.MonitorModel;

public class SNMPCPUOverviewPanel extends SNMPBorderedComponent {

	SNMPPlotterPercentage plotter;

	static final Dimension PREFERRED_PLOTTER_SIZE = new Dimension(300, 200);
	static final Dimension MINIMUM_PLOTTER_SIZE = new Dimension(200, 150);

	Map<String, SNMPPlotterPercentage.Sequence> seqStore = new HashMap<String, SNMPPlotterPercentage.Sequence>();

	// This is the default view range for all the overview plotters
	static final int VIEW_RANGE = -1; // Show all data
	static final boolean IS_GTK = false;
	Random rc = new Random();

	static Color PLOTTER_COLOR = IS_GTK ? new Color(231, 111, 80) : null;

	JLabel infoLabel;
	private ColorTemplates ct = new ColorTemplates();

	public SNMPCPUOverviewPanel() {
		super("CPU Usage");
		setLayout(new BorderLayout(0, 0));

		SNMPPlotterPercentage plotter = new SNMPPlotterPercentage();
		plotter.setPreferredSize(PREFERRED_PLOTTER_SIZE);
		plotter.setMinimumSize(MINIMUM_PLOTTER_SIZE);
		plotter.setViewRange(VIEW_RANGE);
		plotter.setUnit(SNMPPlotterPercentage.Unit.PERCENT);
		setPlotter(plotter);
	}

	public SNMPPlotterPercentage getPlotter() {
		return this.plotter;
	}

	public void setPlotter(SNMPPlotterPercentage plotter) {
		this.plotter = plotter;
		setComponent(plotter);
	}

	public void updateCPUInfo(MonitorModel result) {
		SNMPMonitorModel snmpResult = (SNMPMonitorModel) result;

		Map<String, Double> cpuDetailed = snmpResult.getCPUDetailed();
		long[] values = new long[cpuDetailed.size()];

		int idx = 0;
		for (String cpuName : cpuDetailed.keySet()) {
			SNMPPlotterPercentage.Sequence tempSeq = this.seqStore.get(cpuName
					+ "/processor" + idx);
			if (tempSeq == null) {
				tempSeq = plotter.createSequence(cpuName + "/processor" + idx,
						"CPU Usage", ct.getColor(), true);
				this.seqStore.put(cpuName + "/processor" + idx, tempSeq);
			}
			values[idx] = cpuDetailed.get(cpuName).longValue();
			++idx;
		}
		getPlotter().addValues(snmpResult.getTimestamp(), values);
//		getPlotter().addValues(System.currentTimeMillis(), values);
	}

	public JLabel getInfoLabel() {
		if (infoLabel == null) {
			infoLabel = new JLabel("", CENTER) {
				@Override
				public void setText(String text) {
					if (text.startsWith("<html>")) {
						// Replace spaces with nbsp, except the
						// last one of two or more (to allow wrapping)
						StringBuilder buf = new StringBuilder();
						char[] chars = text.toCharArray();
						int n = chars.length;
						for (int i = 0; i < n; i++) {
							if (chars[i] == ' '
									&& ((i < n - 1 && chars[i + 1] == ' ') || ((i == 0 || chars[i - 1] != ' ') && (i == n - 1 || chars[i + 1] != ' ')))) {
								buf.append("&nbsp;");
							} else {
								buf.append(chars[i]);
							}
						}
						text = buf.toString();
					}
					super.setText(text);
				}
			};

			if (IS_GTK) {
				JPanel southPanel = new JPanel(new BorderLayout());
				JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
				southPanel.add(separator, BorderLayout.NORTH);
				southPanel.add(infoLabel, BorderLayout.SOUTH);
				add(southPanel, BorderLayout.SOUTH);
			} else {
				add(infoLabel, BorderLayout.SOUTH);
			}
		}
		return infoLabel;
	}

	public void clearData() {
		plotter.clearData();

		SNMPPlotterPercentage plotter = new SNMPPlotterPercentage();
		plotter.setPreferredSize(PREFERRED_PLOTTER_SIZE);
		plotter.setMinimumSize(MINIMUM_PLOTTER_SIZE);
		plotter.setViewRange(VIEW_RANGE);
		plotter.setUnit(SNMPPlotterPercentage.Unit.PERCENT);
		setPlotter(plotter);
		this.seqStore.clear();
		ct.resetCounter();
	}

}
